<template>
  <el-form ref="ruleForm" :model="form" label-width="100px" style="margin: 0 auto;width:100%;">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="目录">
          <el-select v-model="form.xftId" style="width:98%;" disabled>
            <template>
              <el-option v-for="item in ggTypes" :key="item.id" :label="item.typeName" :value="item.id" />
            </template>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="文件" prop="gysId">
          <el-upload
            class="upload-demo"
            :action="uploadpath"
            :headers="uoloadheaders"
            :on-success="handleAvatarSuccess"
            :on-change="handleChange"
            :on-remove="handleRemove"
            :on-exceed="handleExceed"
            :file-list="fileList"
            name="uploadfile_ant"
            :limit="1"
          >
            <el-button size="small" icon="el-icon-upload" type="primary">选择文件</el-button>
          </el-upload>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="文件标签" prop="name">
          <el-input v-model="fileLabel" placeholder="文件标签，用';'隔开" />
        </el-form-item>
      </el-col>
    </el-row>
    <div style="text-align:center;">
      <el-button type="primary" @click="save('ruleForm')">提交</el-button>
      <el-button type="danger" @click="closePage">取消</el-button>
    </div>
  </el-form>
</template>

<script>

import { getAllLeafNode } from '@/api/catalogDetail'
import { getInfo, editData } from '@/api/projectFile'
import { getToken } from '@/utils/auth'
import { Message } from 'element-ui'

export default {
  inject: ['getList', 'getTreeList'],
  props: { // 第二种方式
    xiangmu: {
      type: String,
      required: true
    },
    proid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      form: {},
      uploadpath: '',
      name: '',
      oldId: '',
      flag: '',
      fileLabel: '',
      dialogImageUrl: '',
      uoloadheaders: {},
      dialogVisible: false,
      visitPath: null,
      disabled: false,
      ggTypes: [],
      fileData: '', // 文件上传数据（多文件合一）
      fileList: [] // upload多文件数组
    }
  },
  mounted() {
    this.getAllLeafNode()
  },
  methods: {
    getAllLeafNode() {
      getAllLeafNode({ xiangmuId: this.xiangmu }).then(response => {
        this.ggTypes = response.data
      }).catch(function() {
      })
      this.getDetailed()
    },
    getDetailed() {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      getInfo({ code: this.proid }).then(response => {
        this.form = response.data[0]
        this.oldId = response.data[0].id
        this.flag = response.data[0].type
        this.fileLabel = response.data[0].fileLabel
        this.fileList = response.data.map(item => {
          return {
            name: item.name,
            url: item.url
          }
        })

        var path = '/projectFile/uploadFile?xftId=' + this.form.xftId + '&xiangmuId=' + this.form.xiangmuId
        this.uploadpath = process.env.VUE_APP_BASE_API + path
        this.uoloadheaders = { 'X-Token': getToken() }

        loading.close()
      }).catch(function() {
        loading.close()
      })
    },
    handleAvatarSuccess(res, file) {
      this.uploadLoading = false
      if (res.code === 20000) {
        this.form = res.data
        Message({
          message: '上传成功',
          type: 'success',
          duration: 5 * 1000
        })
      } else {
        Message({
          message: res.msg,
          type: 'error',
          duration: 5 * 1000
        })
      }
    },
    // 移除
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    // 选取文件超过数量提示
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
    },
    // 监控上传文件列表
    handleChange(file, fileList) {
      const existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name)
      if (existFile) {
        this.$message.error('当前文件已经存在!')
        fileList.pop()
      }
      this.fileList = fileList
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 确认添加问题
    async save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })

          this.form.id = this.oldId
          this.form.type = this.flag
          this.form.fileLabel = this.fileLabel
          this.form.approvelState = '2'
          editData(this.form).then(response => {
            Message({
              message: '新增成功',
              type: 'success',
              duration: 5 * 1000
            })
            this.$emit('update:visible', false)
            this.getList()
            loading.close()
          }).catch(response => {
            this.getList()
            loading.close()
          })
        } else {
          return false
        }
      })
    },
    closePage() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>

</style>
